package copyengine.network.chat
{
	import copyengine.network.roomStructure.CENetworkCommandNotification;
	import copyengine.network.roomStructure.executeLayer.CENetworkRoomExecuteHandleNotification;
	import copyengine.network.roomStructure.executeLayer.IExecuteHandle;
	import copyengine.network.roomStructure.executeLayer.component.CENetworkExecuteHandleSubEncryptionComponent;
	import copyengine.network.roomStructure.executeLayer.impl.socket.SocketExecuteWarp;
	import copyengine.utils.CENestMediatorBasic;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.debug.CELog;

	import flash.events.Event;
	import flash.utils.ByteArray;

	import org.puremvc.as3.interfaces.INotification;

	/**
	 * 聊天服务器和逻辑服务器不是同一个Socket,所以分开
	 */
	public final class CENetworkChatMediator extends CENestMediatorBasic implements IExecuteHandle
	{
		private var socketExecuteWarp:SocketExecuteWarp;

		//ByteArray加密解密模块
		private var subEncryptionComponent:CENetworkExecuteHandleSubEncryptionComponent;

		private var socketURL:String;
		private var socketProt:int;
		private var playerUUID:String;

		private var isSocketConnect:Boolean;

		public function CENetworkChatMediator(_socketURL:String, _socketProt:uint, _playerUUID:String)
		{
			socketURL = _socketURL;
			socketProt = _socketProt;
			playerUUID = _playerUUID;
			super();
		}

		override public function onRegister():void
		{
			socketExecuteWarp = new SocketExecuteWarp();
			socketExecuteWarp.initialize(this);
			socketExecuteWarp.connect(socketURL, socketProt);

			subEncryptionComponent = new CENetworkExecuteHandleSubEncryptionComponent();
		}

		override public function listNotificationInterests():Array
		{
			return [
				CENetworkChatNotification.RECEIVE_SEND_CHAT_DATA,
				]
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CENetworkChatNotification.RECEIVE_SEND_CHAT_DATA:
					isSocketConnect && onReceiveSendChatData(notification.getBody());
					break;
			}
		}

		private function onReceiveJoinChatServer(_playerUUID:String):void
		{
		}

		private function onReceiveSendChatData(_arg:Object):void
		{
			//原始的用于LTC传送的Arg Object
			var ltcArgOriginal:Object =
				{
					notificationName:_arg["notificationName"],
					notificationArg:_arg["notificationArg"]
				};
			//序列化后的LTC Arg
			var forwardDataObj:Object = {
					LTC:CENetworkCommandNotification.LTC_DATA_LAYER_SYNC_COMMAND,
					arg:GeneralUtils.serializeObject(ltcArgOriginal)
			}

			//加密+压缩数据包
			var forwardDataByteArray:ByteArray = subEncryptionComponent.encryptionForwardData(forwardDataObj);

			var chatType:int = _arg["chatType"];
			var command:uint;
			var commandArg:Object;
			if (chatType == CENetworkChatNotification.CHAT_TYPE_PUBLIC)
			{
				command = CENetworkCommandNotification.F_RESERVE_COMMAND_CHAT_PUBLIC_MSG;
			}
			else
			{
				command = CENetworkCommandNotification.F_RESERVE_COMMAND_CHAT_PRIVATE_MSG;
				commandArg = {uuid:_arg["uuid"]};
			}
			socketExecuteWarp.sendData(command, commandArg, forwardDataByteArray);
		}


		public function onExecuteConnected(_warpType:int):void
		{
			socketExecuteWarp.sendData(CENetworkCommandNotification.F_RESERVE_COMMAND_JOIN_CHAT_SERVER, {uuid:playerUUID}, null);
		}

		public function onReceiveExecuteData(_warpType:int, _command:int, _commandArg:Object = null, _forwardData:ByteArray = null):void
		{
			//如果
			if (_command == CENetworkCommandNotification.S_RESERVE_COMMAND_JOIN_CHAT_SERVER_RETURN)
			{
				var errorCode:uint = _commandArg["errorCode"];
				if (errorCode > 0)
				{
					CELog.err("CENetworkChatMediator->onJoinChatRoomErro Code : " + errorCode);
				}
				else
				{
					isSocketConnect = true;
				}
			}
			else
			{
				var forwardDataObj:Object;
				if (_forwardData && _forwardData.length > 0)
				{
					forwardDataObj = subEncryptionComponent.decryptionForwardData(_forwardData);
				}
				sendNotification(CENetworkRoomExecuteHandleNotification.SEND_RECEIVE_INCOMING_DATA,
								 {
									 command:_command,
									 commandArg:_commandArg,
									 forwardData:forwardDataObj
								 });
			}
		}

		public function onExecuteDisconnected(_warpType:int):void
		{
			CELog.err("CENetworkChatMediator->onExecuteDisconnected chat server on Disconnected");
			isSocketConnect = false;
		}

		public function onExecuteError(_warpType:int, e:Event):void
		{
			CELog.err("CENetworkChatMediator->onExecuteError chat server on Error");
			isSocketConnect = false;
		}
	}
}
